import { login, showModal } from "../../utils/asyncWx.js";
import { reqest, request } from "../../request/index";
const regeneratorRuntime = require("../../lib/runtime/runtime.js");
Page({
  // 获取用户信息
  async handleGetUserInfo(e) {
    console.log(e)
    try {
      const { encryptedData, rawData, iv, signature } = e.detail;
      // 获取小程序登录后的code
      const { code } = await login();
      const loginParams = { encryptedData, rawData, iv, signature, code };
      // 发送请求获取用户token
      // const res = await request({
      //   url: "/users/wxlogin",
      //   data: loginParams,
      //   method: "post"
      // });
      // 测试用，商用时可删除
      // if (res===null){
      //   showModal({content:'测试版本，仅作演示用，无法获取用户token值，正式版请删除此段代码'})
      //   return
      // }
      // 假token
      const {token} = {"user_id": 23,
      "user_email_code": null,
      "is_active": null,
      "user_sex": "男",
      "user_qq": "",
      "user_tel": "",
      "user_xueli": "本科",
      "user_hobby": "",
      "user_introduce": null,
      "create_time": 1562221487,
      "update_time": 1562221487,
      "token": "BearereyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"}
      // 把获取到的token存入到缓存中同时返回上一个界面
      wx.setStorageSync("token", token);
      wx.navigateBack({
        delta: 1
      })
    } catch (error) {
      showModal({
        content:
          "获取token失败"
      }),
      wx.navigateBack({
        delta:1
      })
      return
    }
  }
})
